package com.zw.weiyi.avtivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zw.weiyi.enety.Person;
import com.zw.weiyi.phonesms.BaseActivity;
import com.zw.weiyi.phonesms.R;
import com.zw.weiyi.util.MyApplication;

/**
 * 添加联系人
 * @author Striver_zw
 *  Date 2014-12-05
 */
public class UpPerson extends BaseActivity {
	private EditText p_uppnone;
	private String addpnone;
	private Person person;
	private Button update,back;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.info_updateperson);
	
		initview();
	}

	// init
	public void initview() {
		person = new Person();
		p_uppnone = (EditText) findViewById(R.id.p_uppnone);
		back = (Button) findViewById(R.id.back);
		update = (Button) findViewById(R.id.update);
		Intent intent = this.getIntent();
		person.setId(intent.getLongExtra("uid", 0));
		update.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				try {
					update();
				} catch (Exception e) {
					e.printStackTrace();
				}				
			}
		});
		
		back.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				finish();				
			}
		});
	}

	// 使用事务 写入手机中
	public void update() throws Exception {
		addpnone = p_uppnone.getText().toString();

		if (TextUtils.isEmpty(addpnone) || TextUtils.isEmpty(addpnone)) {
			Toast.makeText(this, "名字或电话不能为空不能", 1000).show();
			return;
		} else {
			updates(addpnone);
			this.finish();
			MyApplication.getInstance().finishActivity(InfoActivity.class);
		}

	}

	public void updates(String addpnone) throws Exception {
		// 更新 只需要更新data里面
		Uri uri = Uri.parse("content://com.android.contacts/data");// 对data表的所有数据操作
		ContentResolver resolver = this.getContentResolver();
		ContentValues values = new ContentValues();
		values.put("data1", addpnone);
		resolver.update(
				uri,
				values,
				"mimetype=? and raw_contact_id=?",
				new String[] { "vnd.android.cursor.item/phone_v2",
						person.getId() + "" });
		Toast.makeText(this, "修改成功", 1000).show();
	}
}
